import { defineStore } from 'pinia'

export interface DashboardMetrics {
  pendingTransactions: number
  pendingMatches: number
  accuracyRate: number
  transactionTrend: number
  matchTrend: number
  accuracyTrend: number
  highConfidenceMatches: number
  pendingAmount: number
  totalCustomers: number
  monthlyAmount: number
  efficiencyScore: number
  customerTrend: number
  amountTrend: number
}

export interface AIInsight {
  id: string
  title: string
  description: string
  priority: 'high' | 'medium' | 'low'
  confidence?: number
  time: string
  action?: string
  actionText?: string
}

export interface TodoItem {
  id: string
  title: string
  description: string
  type: 'reconciliation' | 'review'
  priority: 'high' | 'medium' | 'low'
  completed: boolean
  dueDate?: string
  actionUrl?: string
}

export interface Activity {
  id: string
  type: 'reconciliation' | 'import' | 'system'
  title: string
  description: string
  time: string
  user?: string
  metadata?: any
}

export const useDashboardStore = defineStore('dashboard', {
  state: () => ({
    metrics: {} as DashboardMetrics,
    insights: [] as AIInsight[],
    todos: [] as TodoItem[],
    activities: [] as Activity[],
    notifications: [] as any[]
  }),

  actions: {
    async getDashboardMetrics(): Promise<DashboardMetrics> {
      // 模拟API调用
      const mockData: DashboardMetrics = {
        pendingTransactions: 24,
        pendingMatches: 12,
        accuracyRate: 96.5,
        transactionTrend: 5.2,
        matchTrend: 3.8,
        accuracyTrend: 1.5,
        highConfidenceMatches: 7,
        pendingAmount: 125000,
        totalCustomers: 42,
        monthlyAmount: 2850000,
        efficiencyScore: 88,
        customerTrend: 8.3,
        amountTrend: 12.7
      }
      
      this.metrics = mockData
      return mockData
    },

    async getTrendData(period: string): Promise<any[]> {
      // 模拟API调用
      const mockData = [
        { date: '2023-01-01', transactions: 120, matches: 95, accuracy: 92 },
        { date: '2023-01-02', transactions: 135, matches: 110, accuracy: 94 },
        { date: '2023-01-03', transactions: 110, matches: 90, accuracy: 91 },
        { date: '2023-01-04', transactions: 145, matches: 125, accuracy: 95 },
        { date: '2023-01-05', transactions: 160, matches: 140, accuracy: 96 },
        { date: '2023-01-06', transactions: 130, matches: 115, accuracy: 93 },
        { date: '2023-01-07', transactions: 155, matches: 135, accuracy: 95 }
      ]
      return mockData
    },

    async getAIInsights(): Promise<AIInsight[]> {
      // 模拟API调用
      const mockData: AIInsight[] = [
        {
          id: '1',
          title: '异常交易检测',
          description: '检测到3笔异常交易，建议人工审核',
          priority: 'high',
          confidence: 92,
          time: '2小时前',
          action: 'review',
          actionText: '查看详情'
        },
        {
          id: '2',
          title: '匹配优化',
          description: '发现5个高置信度匹配机会，可提升对账效率',
          priority: 'low',
          confidence: 78,
          time: '1天前',
          action: 'match',
          actionText: '查看建议'
        }
      ]
      
      this.insights = mockData
      return mockData
    },

    async getQuickTodos(): Promise<TodoItem[]> {
      // 模拟API调用
      const mockData: TodoItem[] = [
        {
          id: '1',
          title: '审核异常交易',
          description: '3笔异常交易需要人工审核',
          type: 'reconciliation',
          priority: 'high',
          completed: false,
          dueDate: '2023-01-10'
        },
        {
          id: '2',
          title: '匹配确认',
          description: '5个高置信度匹配待确认',
          type: 'review',
          priority: 'medium',
          completed: false,
          dueDate: '2023-01-12'
        }
      ]
      
      this.todos = mockData
      return mockData
    },

    async getRecentActivities(): Promise<Activity[]> {
      // 模拟API调用
      const mockData: Activity[] = [
        {
          id: '1',
          type: 'reconciliation',
          title: '完成对账',
          description: '成功匹配25笔交易',
          time: '10分钟前',
          user: '张三'
        },
        {
          id: '2',
          type: 'import',
          title: '数据导入',
          description: '成功导入50条银行流水',
          time: '2小时前',
          user: '王五'
        },
        {
          id: '3',
          type: 'system',
          title: '系统更新',
          description: '完成月度数据备份',
          time: '1天前',
          user: '系统'
        }
      ]
      
      this.activities = mockData
      return mockData
    },

    async getNotifications(): Promise<any[]> {
      // 模拟API调用
      const mockData = [
        {
          id: '1',
          title: '系统维护通知',
          content: '系统将于今晚02:00-04:00进行维护',
          time: '2023-01-05 14:30',
          read: false
        }
      ]
      
      this.notifications = mockData
      return mockData
    },

    async completeTodo(todoId: string): Promise<void> {
      // 模拟API调用
      const todo = this.todos.find((t: TodoItem) => t.id === todoId)
      if (todo) {
        todo.completed = true
      }
      // 这里应该调用API更新状态
      // await api.post(`/dashboard/todos/${todoId}/complete`)
    }
  }
})